<?php
 global $objTemplate, $arrConfig,$sCanEdit,$lang;
  $sCanEdit = 1;  
    $objXajax->registerFunction("uploadfile");
  # start region process data   

  function uploadfile($file){
    global $lang,$objDbSelect,$path;
    $objResponse = new xajaxResponse();
/*       $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;
   
   $target_path = $destination_path . basename( $_FILE['myfile']['name']);
   //echo $_FILE['myfile']['name']; die();
   if(@move_uploaded_file($_FILE['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }
   
   sleep(1);
               */
    return $objResponse->getXML();    
  }
  
 function  frmimportexcel_GetGrid(){
    global $objDbSelect,$lang;
    $sqlSelect = "select t.* from tbl_tablescript as t ";
    return $objDbSelect->GetArray($sqlSelect);
} 

function frmimportexcel_create($from,$to){
     global $objDbSelect;
     $objResponse = new xajaxResponse();
     $sql = "SELECT * FROM tbl_tablescript WHERE  active = 1";
     $arr = $objDbSelect->GetArray($sql);
     $output ='';
     for($j>$from;$j<=$to;$j++){
        for($i=0;$i<count($arr);$i++){
          $table_name = $arr[$i]['table_name'];  
          $table_script = $arr[$i]['table_script']; 
          $sqlcreate =  str_replace($table_name,$table_name.'_'.$j,$table_script) ;
          // echo  $sqlcreate; die();
          $result = $objDbSelect->Execute($sqlcreate);
          if(!$result){
              $output .=  ",$table_name".'_'.$j;
          }          
        }    
     }
     $objResponse->addScript('changeWaiting(1);');
     return $objResponse->getXML();    
    // return $output;
}

function frmimportexcel_drop($from,$to){
     global $objDbSelect;
     $sql = "SELECT * FROM tbl_tablescript WHERE active = 1";
     $arr = $objDbSelect->GetArray($sql);
     $output ='';
     for($j>$from;$j<=$to;$j++){
        for($i=0;$i<count($arr);$i++){
          $table_name = $arr[$i]['table_name'];  
          $sqldrop = "DROP TABLE IF EXISTS $table_name"."_$j";
          $result = $objDbSelect->Execute($sqldrop);
          if(!$result){
              $output .=  ",$table_name".$j;
          }          
        }    
     }
     $objResponse->addScript('changeWaiting(1);');
     return $objResponse->getXML();    
     //return $output;
}

function frmimportexcel_save($param){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $tablename = $param['txtTableName'];
    $tablescript =addslashes(str_replace("\\",'',$param['txtTableScript'])) ;
    $edit = $param['hdEdit'];
    //rvice_type = $param['cbbService'];
    //echo '<pre>'; print_r($param); die();
    $id = $param['hdId'];
    if($edit!=1){
       $sql = "insert into tbl_tablescript(table_name,table_script) value('$tablename','$tablescript')" ; 
       $objDbSelect->Execute($sql);
      // echo 
       $id_insert = mysql_insert_id();
        $objResponse->addScript("changeUpdate('frmimportexcel',$id_insert);");     
    }else{
       $sql = "UPDATE tbl_tablescript SET table_name='$tablename',table_script='$tablescript' WHERE id = $id"; 
       //echo  $sql;
       $objDbSelect->Execute($sql);
    }
    $objResponse->addScript('xajax_frmimportexcel_loadGrid();');  
    $objResponse->addScript('changeWaiting(1);');    
    return $objResponse->getXML();      
}

function  frmimportexcel_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_tablescript where id = '$id'";
    $arr         = $objDbSelect->GetArray($sql);
    
    $name_vi = $arr[0]['table_name'];
    $script = addslashes($arr[0]['table_script']);
    //$service_type = $arr[0]['service_type'];       
    $objResponse->addScript("$('#frmimportexcel_txtTableName').val('$name_vi');");
    $objResponse->addScript("$('#frmimportexcel_txtTableScript').val('$script');"); 
    //$objResponse->addScript("$('#frmimportexcel_cbbService').val('$service_type');"); 
    $objResponse->addAssign('frmimportexcel_txtTableScript','value',$script); 
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
}

/*function frmimportexcel_getService(){
    global $objDbUpdate,$lang;
    $sql = "select id, name_$lang as name from tbl_service_type";
    $arr = $objDbUpdate->GetArray($sql);
    return $arr;
} */
  
  # end region process data
?>
